import LXCOM from '../common/common.js'
import absgraph from '../graph/absgraph.js'
import fillinfo from "../common/struct/fillinfo";

import { LGXEnums } from '../common/sysenum.js'

var fillTypeEnums = LGXEnums.LGXFillStyle;

import GlobalConst from '../common/globalConst.js'
var global = GlobalConst.GlobalConst;

import linegradientutil from './paintutil/linegradientutil.js';
import { GlobalVar } from '../common/globalvar.js';

import utilfuncs from '../common/tools/utilfuncs.js';
import arrowManager from './paintutil/arrowManager.js';
var funcs = utilfuncs.UtilFuncs;

class BaseUnitGraph extends absgraph.AbsGraph {
    constructor(type) {
        super(type);
        this._lineDef = new LXCOM.LGXLineDefInfo();
        this._lineDef.color.clone(GlobalVar.lineInfo.color);
        this._lineDef.lineWidth = GlobalVar.defaultLineWidth;

        this._fillDef = new fillinfo.LGXFillDefInfo();

        //下面参数为作为模具图元时的控制参数
        this.hornSizeCtrlFlag = 0;//1-尺寸不变 0-随模具主容器矩形 2-自由调整
        this.hornPostionCtrlFlag = 0;//尺寸不变时横向位置控制参数，0-左侧跟随容器 1--右侧跟随容器

        this.verticalSizeCtrlFlag = 0;//1-尺寸不变 0-随模具主容器矩形 2-自由调整
        this.verticalPostionCtrlFlag = 0;//尺寸不变时横向位置控制参数，0-顶部跟随容器 1--底部跟随容器
        this.disInSymbolFlag = 0;
        this.labelCtrlFlag = 1;
    }

    display(map, mapInfo, forClip) {

    }

    setLineDef(v) { this._lineDef.clone(v); }
    getLineDef() { return this._lineDef; }

    setFillDef(v) { this._fillDef.clone(v); }
    getFillDef() { return this._fillDef; }

    //hornSize,hornPos,verticalSize,verticalPos
    setSymbolUnitCtrlInfo(unitCtlInfo) {
        this.hornSizeCtrlFlag = parseInt(unitCtlInfo.hornSize);
        this.hornPostionCtrlFlag = parseInt(unitCtlInfo.hornPos);
        this.verticalSizeCtrlFlag = parseInt(unitCtlInfo.verticalSize);
        this.verticalPostionCtrlFlag = parseInt(unitCtlInfo.verticalPos);
    }

    getSymbolUnitCtrlInfo() {
        let hornSize = this.hornSizeCtrlFlag;
        let hornPos = this.hornPostionCtrlFlag;
        let verticalSize = this.verticalSizeCtrlFlag;
        let verticalPos = this.verticalPostionCtrlFlag;
        let unitCtlInfo = { hornSize, hornPos, verticalSize, verticalPos };
        return unitCtlInfo;
    }

    setHornSizeCtrlFlag(v) { this.hornSizeCtrlFlag = v; }
    getHornSizeCtrlFlag() { return this.hornSizeCtrlFlag; }

    setHornPostionCtrlFlag(v) { this.hornPostionCtrlFlag = v; }
    getHornPostionCtrlFlag() { return this.hornPostionCtrlFlag; }

    setVerticalSizeCtrlFlag(v) { this.verticalSizeCtrlFlag = v; }
    getVerticalSizeCtrlFlag() { return this.verticalSizeCtrlFlag; }

    setVerticalPostionCtrlFlag(v) { this.verticalPostionCtrlFlag = v; }
    getVerticalPostionCtrlFlag() { return this.verticalPostionCtrlFlag; }


    assignInfo(info) {
        super.initCommProp(info);
        this._lineDef.color.setColorByValue(info.color);
        this._lineDef.linemode = info.lineMode;
        this._lineDef.joinmode = info.lineJoin;
        this._lineDef.capmode = info.lineCap;
        this._lineDef.lineWidth = info.lineWidth;
        this._lineDef.stArrowType = info.stArrowType;
        this._lineDef.endArrowType = info.endArrowType;

        this._fillDef.fillStyle = info.fillMode;
        if (info.fillInfo) {
            this._fillDef.clone(info.fillInfo);
        }

        this.hornSizeCtrlFlag = info.hornSizeCtrlFlag;
        if (!this.hornSizeCtrlFlag) {
            this.hornSizeCtrlFlag = 0;
        }
        this.hornPostionCtrlFlag = info.hornPostionCtrlFlag;
        if (!this.hornPostionCtrlFlag) {
            this.hornPostionCtrlFlag = 0;
        }

        this.verticalSizeCtrlFlag = info.verticalSizeCtrlFlag;
        if (!this.verticalSizeCtrlFlag) {
            this.verticalSizeCtrlFlag = 0;
        }

        this.verticalPostionCtrlFlag = info.verticalPostionCtrlFlag;
        if (!this.verticalPostionCtrlFlag) {
            this.verticalPostionCtrlFlag = 0;
        }
        this.disInSymbolFlag = info.disInSymbolFlag;
        if (!this.disInSymbolFlag) {
            this.disInSymbolFlag = false;
        }

    }

    modifyAttribute(info, mapscale) {
        super.modifyCommonAttribute(info);

        this._lineDef.color.setColorByValue(info.color);
        this._lineDef.linemode = info.lineMode;
        this._lineDef.joinmode = info.lineJoin;
        this._lineDef.capmode = info.lineCap;
        this._lineDef.lineWidth = info.lineWidth;
        this._lineDef.stArrowType = info.stArrowType;
        this._lineDef.endArrowType = info.endArrowType;

        this._fillDef.fillStyle = info.fillMode;
        this._fillDef.clone(info.fillInfo);
    }

    toInfo(info) {

    }

    toInfo4InheritClass(info) {
        super.commoneProp2Info(info);
        // console.log("toinfo----------------------------------------------");
        info.color = this._lineDef.color.toValue();
        info.lineMode = this._lineDef.linemode;
        info.lineJoin = this._lineDef.joinmode;
        info.lineCap = this._lineDef.capmode;
        info.lineWidth = this._lineDef.lineWidth;

        info.fillMode = this._fillDef.fillStyle;
        info.fillInfo = new fillinfo.LGXFillDefInfo();
        info.fillInfo.clone(this._fillDef);

        info.stArrowType = this._lineDef.stArrowType;
        info.endArrowType = this._lineDef.endArrowType;

        info.hornSizeCtrlFlag = this.hornSizeCtrlFlag;
        info.hornPostionCtrlFlag = this.hornPostionCtrlFlag;
        info.verticalSizeCtrlFlag = this.verticalSizeCtrlFlag;
        info.verticalPostionCtrlFlag = this.verticalPostionCtrlFlag;
        info.disInSymbolFlag = this.disInSymbolFlag;
        return;
    }

    getSelCmpGap(scaling, mapscale) {
        var nLineWidth = this._lineDef.lineWidth;
        var disScale = scaling / mapscale;
        var nTmpLineWid = nLineWidth * disScale * 0.5;
        var cmpGap = global.SelectGap + nTmpLineWid;
        return cmpGap;
    }

    getLineInfoDesc() {
        let color = this._lineDef.color.toValue();
        let lineMode = this._lineDef.linemode;
        let lineJoin = this._lineDef.joinmode;
        let lineCap = this._lineDef.capmode;
        let lineWidth = this._lineDef.lineWidth;

        let arrowMng = new arrowManager.LGXArrowManager();

        let stArrowType = 0;
        let arrowInfo = arrowMng.getArrowInfo(this._lineDef.stArrowType);
        if (arrowInfo) {
            stArrowType = arrowInfo.index;
        }

        let endArrowType = 0;
        arrowInfo = arrowMng.getArrowInfo(this._lineDef.endArrowType);
        if (arrowInfo) {
            endArrowType = arrowInfo.index;
        }

        let lineVisionType = "straight";
        if (this.m_graphType == LGXEnums.LGXGraphType.LineGraph_type) {
            lineVisionType = this.visionType;
        }
        return { color, lineMode, lineJoin, lineCap, lineWidth, stArrowType, endArrowType, lineVisionType };
    }

    getFillDefInfo() {
        let fillStyle = "none"
        let childmode = "";
        let fillColor = 0;
        let gradientInfo = null;

        var fillInfo = this.getFillDef();
        if (fillInfo.fillStyle == 0) {
            fillStyle = "none";
        }
        else if (fillInfo.fillStyle == 1) {
            fillStyle = "solid";
            fillColor = fillInfo.fillColor.toValue();
        }

        else if (fillInfo.fillStyle == LGXEnums.LGXFillStyle.lineGradient_complex) {
            fillStyle = "Gradient";
            childmode = "lineGradient";
            gradientInfo = fillInfo.gradientInfo;
        }
        else if (fillInfo.fillStyle == LGXEnums.LGXFillStyle.radialGradient_complex) {
            fillStyle = "Gradient";
            childmode = "radialGradient";
            gradientInfo = fillInfo.gradientInfo;
        }


        if (!gradientInfo) {
            gradientInfo = new fillinfo.GradientInfo();
        }

        return { fillStyle, childmode, fillColor, gradientInfo };
    }

    getMultiPointGraphLocalZone() {
        let mtx = this.getLocalMatrix();
        let inverseMtx = mtx.Inverse();
        let rect = new LXCOM.LGXRect(0, 0, 0, 0);
        let position = this.getPos();
        let length = position.posList.length;
        for (var i = 0; i < length; i++) {
            let pt = position.posList[i];
            let localPt = inverseMtx.MultT({ x: pt.x, y: pt.y, z: 0 });//求相对于控制图形局部坐标系的局部坐标  
            let x = localPt.x;
            let y = localPt.y;

            if (i == 0) {
                rect.left = x;
                rect.right = x;
                rect.top = y;
                rect.bottom = y;
            }
            rect.left = Math.min(rect.left, x);
            rect.right = Math.max(rect.right, x);
            rect.bottom = Math.min(rect.bottom, y);
            rect.top = Math.max(rect.top, y);
        }
        return rect;
    }

    chgGradientPos() {
        console.log("chgGradientPos*****************************");
        let fillDef = this._fillDef;
        if (fillDef.userSetPosFlag) {
            return;
        }
        var poslist = null;
        if (fillDef.fillStyle == fillTypeEnums.radialGradient_complex) {
            //let util = radialgradientutil.RadialGradientUtil;
            // poslist = util.getGradientCoord(fillDef.dir, this, mapinfo, false);
        }
        else {
            let util = linegradientutil.LineGradientUtil;
            poslist = util.getGradientCoord_World(fillDef.dir, this, false);
        }
        if (poslist && poslist.length == 2) {
            let p1 = poslist[0];
            let p2 = poslist[1];

            console.log(p1);
            console.log(p2);

            fillDef.stPt.x = p1.x;
            fillDef.stPt.y = p1.y;

            fillDef.endPt.x = p2.x;
            fillDef.endPt.y = p2.y;
        }
    }

    translateGradientFill(tOffset) {
        // this._fillDef.translate(tOffset.x, tOffset.y);
    }

    rotateGradientFill(rotCnt, radAngle) {
        // this._fillDef.rotate(rotCnt, radAngle);
    }

    //精确判断图形与目标矩形相交
    chkExactIntersectRect(rect) {
        if (this.getPosType() != LGXEnums.LGXPosMode.multiPt_Mode) {
            return false;
        }
        let position = this.getPosCopy();
        let posList = position.posList;
        if (this.chkClose()) {
            posList.push(posList[0]);
        }
        let p0 = posList[0];
        for (let i = 1; i < posList.length; i++) {
            let p1 = posList[i];
            let rst = funcs.chkLineSecInterRect(p0, p1, rect);
            if (rst && rst.flag) {
                return true;
            }
            p0 = p1;
        }
        return false;
    }

    copyCommnonPropFromBuf(bufInfo, tx, ty) {
        let lineDef = this.getLineDef();
        lineDef.color.setColorByValue(bufInfo.color);
        lineDef.linemode = bufInfo.lineMode;
        lineDef.joinmode = bufInfo.lineJoin;
        lineDef.capmode = bufInfo.lineCap;
        lineDef.lineWidth = bufInfo.lineWidth;
        lineDef.stArrowType = bufInfo.stArrowType;
        lineDef.endArrowType = bufInfo.endArrowType;

        if (bufInfo.fillInfo) {
            let fillDef = this.getFillDef();
            fillDef.clone(bufInfo.fillInfo);
        }
        else {
            console.log("none fill");
        }
        // fillDef.translate(tx, ty);
    }

    drag2ModifyGradientInfo(newTransform) {
        let fillDef = this.getFillDef();
        if (!fillDef.gradientInfo) {
            return;
        }
        let curentMtx = this.getLocalMatrix();
        let curentInverseMtx = curentMtx.Inverse();

        let stpt = fillDef.gradientInfo.stPt;
        let endpt = fillDef.gradientInfo.endPt;
        let newWolrdPt1 = newTransform.newDestMtx.MultT({ x: stpt.x, y: stpt.y, z: 0 });
        let newLocalPt1 = curentInverseMtx.MultT(newWolrdPt1);
        fillDef.gradientInfo.stPt.x = newLocalPt1.x;
        fillDef.gradientInfo.stPt.y = newLocalPt1.y;

        let newWolrdPt2 = newTransform.newDestMtx.MultT({ x: endpt.x, y: endpt.y, z: 0 });
        let newLocalPt2 = curentInverseMtx.MultT(newWolrdPt2);
        fillDef.gradientInfo.endPt.x = newLocalPt2.x;
        fillDef.gradientInfo.endPt.y = newLocalPt2.y;

    }
}

export default { BaseUnitGraph }
